home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / grafix / tools / amipeg_0.4 / s24bit.s < prev    next >
Text File  |  1994-04-22  |  9KB  |  480 lines

  1. ;
  2. ; This source handles conversion from yuv to rgb space, which includes
  3. ; initialization of the conversion & clamp tables.
  4. ;
  5. ; Original routines are in 24bit.c, which mainly contained those lovely 
  6. ; just-perfectly-fit-for-assembler-kick-ass constructions.
  7. ;
  8. ; Michael Rausch  14-4-94  1:12:57
  9. ;
  10.  
  11.     SECTION    text,CODE
  12.  
  13. ;************************************************************************************
  14.  
  15. ; We'll define the "ConvertColor" macro here to do fixed point arithmetic
  16. ; that'll convert from YCrCb to RGB using:
  17. ;    R = L + 1.40200*Cr
  18. ;    G = L - 0.34414*Cb - 0.71414*Cr
  19. ;    B = L + 1.77200*Cb
  20. ;
  21.  
  22. ; void InitColorDither(void)
  23.     XDEF    @InitColorDither
  24. @InitColorDither:            
  25.  
  26.     lea    Cb_r_tab,a0
  27. Cr_gb_off EQU    256*4
  28.     lea    Cb_r_tab+Cr_gb_off,a1
  29.  
  30.     move.w    #255,d0
  31.     moveq    #-128,d1
  32. crgbtabs:
  33.     move.w    d1,d2
  34.     muls.w    #179,d2        ; 1.40200
  35.     asr.w    #7,d2
  36.     add.w    d2,d2
  37.     move.w    d2,(a0)+    ;br
  38.     move.w    d1,d2
  39.     muls.w    #-91,d2        ; -0.71414
  40.     asr.w    #7,d2
  41.     add.w    d2,d2
  42.     move.w    d2,(a1)+    ;bg
  43.     move.w    d1,d2
  44.     muls.w    #-44,d2        ; -0.34414
  45.     asr.w    #7,d2
  46.     add.w    d2,d2
  47.     move.w    d2,(a0)+    ;rg
  48.     move.w    d1,d2
  49.     muls.w    #226,d2        ; 1.77200
  50.     asr.w    #7,d2
  51.     add.w    d2,d2
  52.     move.w    d2,(a1)+    ;rb
  53.     addq.l    #1,d1
  54.     dbra    d0,crgbtabs
  55.  
  56. ;
  57. ; create the clamp tables, including the shifted one
  58. ;
  59. ; IMPORTANT: The first versions had a MUCH too low clampsize, resulting in really
  60. ; annoying purple artifacts !!!
  61. ;
  62. clampsize EQU    192
  63.  
  64.     moveq    #0,d1
  65.     lea    _clamp-clampsize*2,a0            ; clear lower and upper 64 bytes
  66.     move.w    #clampsize-1,d0
  67. fill_ct1:move.w    #$ff00,((clampsize+256)*2,a0)
  68.     move.w    d1,(a0)+
  69.     dbra    d0,fill_ct1
  70.     move.w    #255,d0
  71. fill_ct2:move.w    d1,(a0)+
  72.     add.w    #$100,d1
  73.     dbra    d0,fill_ct2
  74.  
  75.     rts
  76.  
  77. ;************************************************************************************
  78.  
  79. ;void ColorDitherImage(unsigned char *lum, unsigned char *cr, unsigned char *cb,
  80. ;              unsigned char *out, int rows, int cols)
  81.     XDEF @ColorDitherImage_lores
  82. @ColorDitherImage_lores:
  83.  
  84. ;    a0  unsigned char *lum
  85. ;    a1  unsigned char *cr
  86. ; 4(sp)  unsigned char *cb
  87. ; 8(sp)  unsigned char *out
  88. ;    d0  int rows
  89. ;    d1  int cols
  90.  
  91. cdi_regs REG    d2-d7/a2-a6    ; 6+5=11
  92.     movem.l    cdi_regs,-(sp)
  93.  
  94. ;saved7    EQU    0
  95. saverows EQU    4
  96. savecols EQU    8
  97. localvars EQU    savecols+4
  98. cb_offset EQU    localvars+11*4+4
  99. out_offset EQU    cb_offset+4
  100.  
  101.     sub.w    #localvars,sp
  102.  
  103.     move.l    d1,(savecols,sp)
  104.     lsr.l    #1,d0
  105.     subq.l    #1,d0
  106.     move.l    d0,d7
  107.  
  108.     move.l    a0,a2            ; lum1
  109.     move.l    a2,a3
  110.     add.l    d1,a3            ; lum2 = lum1+cols
  111.  
  112.     move.l    a1,a4            ; cr channel
  113.     move.l    cb_offset(sp),d5
  114.     sub.l    a4,d5            ; cb channel is addressed via its offset to the cr channel
  115.  
  116.     lea    Cb_r_tab,a5        ; conversion tab(s)
  117.  
  118.     move.l    (out_offset,sp),a0    ; row1
  119.     move.l    a0,a1
  120.     add.l    d1,d1            
  121.     add.l    d1,a1            ; row2=row1+cols*2
  122.  
  123. all_rows_lores:
  124.     move.l    (savecols,sp),d6
  125.     lsr.l    #1,d6
  126.     subq.l    #1,d6
  127. all_cols_lores:
  128.  
  129. ; free: d4
  130.  
  131.     moveq    #0,d1
  132.     moveq    #0,d2
  133.     move.b    (a4,d5.l),d1        ; CB channel
  134.     move.b    (a4)+,d2        ; CR channel
  135.  
  136.     move.l    (a5,d1.w*4),d0            ; cb_r cb_g
  137.     move.l    (Cr_gb_off,a5,d2.w*4),d1    ; cr_g cr_b
  138.  
  139.     move.w    d1,d2
  140.     swap    d1
  141.     add.w    d0,d1
  142.     swap     d0
  143.  
  144.  
  145.     sub.w    d1,d0
  146.     sub.w    d1,d2
  147.  
  148.     ext.l    d1
  149.     add.l    #_clamp,d1
  150.  
  151. ; d0   cb_r      *2
  152. ; d1  (cr_g+cb_g) *2
  153. ; d2   cr_b      *2
  154.  
  155.     moveq    #0,d3            ; *lum++
  156.     move.b    (a2)+,d3
  157.     move.l    d1,a6            ; clamp table includes on L
  158.     add.l    d3,d3
  159.     add.l    d3,a6
  160.     move.w    (a6,d0.w),d3        ; r
  161.     or.b    (a6),d3            ; g
  162.  
  163.     swap    d3
  164.  
  165.     move.b    (a2)+,d3        ; *lum++
  166.     move.l    d1,a6            ; clamp table includes on L
  167.     add.w    d3,d3
  168.     add.w    d3,a6
  169.     move.w    (a6,d2.w),d3        ; b
  170.     or.b    (a6),d3            ; g
  171.  
  172.     move.l    d3,(a0)+        ; *row1++
  173.  
  174.  
  175.  
  176.     moveq    #0,d3            ; *lum++
  177.     move.b    (a3)+,d3
  178.     move.l    d1,a6            ; clamp table includes on L
  179.     add.l    d3,d3
  180.     add.l    d3,a6
  181.     move.w    (a6,d0.w),d3        ; r
  182.     move.b    (a6),d3            ; g
  183.  
  184.     swap    d3
  185.  
  186.     move.b    (a3)+,d3        ; *lum++
  187.     move.l    d1,a6            ; clamp table includes on L
  188.     add.w    d3,d3
  189.     add.w    d3,a6
  190.     move.w    (a6,d2.w),d3        ; b
  191.     move.b    (a6),d3            ; g
  192.  
  193.     move.l    d3,(a1)+        ; *row1++
  194.  
  195.  
  196.  
  197.     dbra    d6,all_cols_lores
  198.  
  199.     move.l    savecols(sp),d0        ; next line in the luminance channel
  200.     add.l    d0,a2
  201.     add.l    d0,a3
  202.  
  203.     add.l    d0,d0            ; 2 bytes per pixel
  204.     add.l    d0,a0            ; next line in the output row
  205.     add.l    d0,a1
  206.  
  207.     dbra    d7,all_rows_lores
  208.  
  209.     add.w    #localvars,sp
  210.     movem.l    (sp)+,cdi_regs
  211.     rts
  212.  
  213. ;************************************************************************************
  214.  
  215.     XDEF @ColorDitherImage
  216. @ColorDitherImage:
  217.     movem.l    cdi_regs,-(sp)
  218.  
  219.     sub.w    #localvars,sp
  220.  
  221.     move.l    d1,(savecols,sp)
  222.     lsr.l    #1,d0
  223.     subq.l    #1,d0
  224.     move.l    d0,d7
  225.  
  226.     move.l    a0,a2            ; lum1
  227.     move.l    a2,a3
  228.     add.l    d1,a3            ; lum2 = lum1+cols
  229.  
  230.     move.l    a1,a4            ; cr channel
  231.     move.l    cb_offset(sp),d5
  232.     sub.l    a4,d5            ; cb channel is addressed via its offset to the cr channel
  233.  
  234.     lea    Cb_r_tab,a5        ; conversion tab(s)
  235.  
  236.     move.l    (out_offset,sp),a0    ; row1
  237.     move.l    a0,a1
  238.     lsl.l    #2,d1
  239.     add.l    d1,a1            ; row2=row1+cols*4
  240.  
  241.  
  242. all_rows:
  243.     move.l    (savecols,sp),d6
  244.     lsr.l    #1,d6
  245.     subq.l    #1,d6
  246. all_cols:
  247.  
  248. ; still free: d4
  249.  
  250.     moveq    #0,d1
  251.     moveq    #0,d2
  252.     move.b    (a4,d5.l),d1        ; CB channel
  253.     move.b    (a4)+,d2        ; CR channel
  254.  
  255.     move.l    (a5,d1.w*4),d0            ; cb_r cb_g
  256.     move.l    (Cr_gb_off,a5,d2.w*4),d1    ; cr_g cr_b
  257.  
  258.     move.w    d1,d2
  259.     swap    d1
  260.     add.w    d0,d1
  261.     swap     d0
  262.  
  263.     sub.w    d2,d0
  264.     sub.w    d2,d1
  265.  
  266.     ext.l    d2
  267.     add.l    #_clamp,d2
  268.  
  269. ; d0   cb_r      *2
  270. ; d1  (cr_g+cb_g) *2
  271. ; d2   cr_b      *2
  272.  
  273.     moveq    #0,d3            ; *lum++
  274.     move.b    (a2)+,d3
  275.     move.l    d2,a6            ; clamp table includes on L
  276.     add.l    d3,a6
  277.     add.l    d3,a6
  278.     move.w    (a6),d3            ; b    BRG0
  279.     move.b    (a6,d0.w),d3        ; r
  280.     swap    d3
  281.     move.w    (a6,d1.w),d3        ; g
  282.     move.l    d3,(a0)+        ; *row1++
  283.  
  284.     moveq    #0,d3            ; *lum++
  285.     move.b    (a2)+,d3
  286.     move.l    d2,a6            ; clamp table includes on L
  287.     add.l    d3,a6
  288.     add.l    d3,a6
  289.     move.w    (a6),d3            ; b    BRG0
  290.     move.b    (a6,d0.w),d3        ; r
  291.     swap    d3
  292.     move.w    (a6,d1.w),d3        ; g
  293.     move.l    d3,(a0)+        ; *row1++
  294.  
  295.  
  296.     moveq    #0,d3            ; *lum2++
  297.     move.b    (a3)+,d3
  298.     move.l    d2,a6            ; clamp table includes on L
  299.     add.l    d3,a6
  300.     add.l    d3,a6
  301.     move.w    (a6),d3            ; b    BRG0
  302.     move.b    (a6,d0.w),d3        ; r
  303.     swap    d3
  304.     move.w    (a6,d1.w),d3        ; g
  305.     move.l    d3,(a1)+        ; *row2++
  306.  
  307.     moveq    #0,d3            ; *lum2++
  308.     move.b    (a3)+,d3
  309.     move.l    d2,a6            ; clamp table includes on L
  310.     add.l    d3,a6
  311.     add.l    d3,a6
  312.     move.w    (a6),d3            ; b    BRG0
  313.     move.b    (a6,d0.w),d3        ; r
  314.     swap    d3
  315.     move.w    (a6,d1.w),d3        ; g
  316.     move.l    d3,(a1)+        ; *row2++
  317.  
  318.  
  319.     dbra    d6,all_cols
  320.  
  321.     move.l    savecols(sp),d0        ; next line in the luminance channel
  322.     add.l    d0,a2
  323.     add.l    d0,a3
  324.  
  325.     lsl.l    #2,d0            ; 4 bytes per pixel
  326.     add.l    d0,a0            ; next line in the output row
  327.     add.l    d0,a1
  328.  
  329.     dbra    d7,all_rows
  330.  
  331.     add.w    #localvars,sp
  332.     movem.l    (sp)+,cdi_regs
  333.     rts
  334.  
  335.  
  336. ;************************************************************************************
  337.  
  338.     XDEF @ColorDitherImage_12bit
  339. @ColorDitherImage_12bit:
  340.     movem.l    cdi_regs,-(sp)
  341.  
  342.     sub.w    #localvars,sp
  343.  
  344.     move.l    d1,(savecols,sp)
  345.     lsr.l    #1,d0
  346.     subq.l    #1,d0
  347.     move.l    d0,d7
  348.  
  349.     move.l    a0,a2            ; lum1
  350.     move.l    a2,a3
  351.     add.l    d1,a3            ; lum2 = lum1+cols
  352.  
  353.     move.l    a1,a4            ; cr channel
  354.     move.l    cb_offset(sp),d5
  355.     sub.l    a4,d5            ; cb channel is addressed via its offset to the cr channel
  356.  
  357.     lea    Cb_r_tab,a5        ; conversion tab(s)
  358.  
  359.     move.l    (out_offset,sp),a0    ; row1
  360.     move.l    a0,a1
  361.     add.l    d1,a1            ; row2=row1+cols
  362.  
  363. all_rows_12bit:
  364.     move.l    (savecols,sp),d6
  365.     lsr.l    #1,d6
  366.     subq.l    #1,d6
  367. all_cols_12bit:
  368.  
  369.     moveq    #0,d1
  370.     moveq    #0,d2
  371.     move.b    (a4,d5.l),d1        ; CB channel
  372.     move.b    (a4)+,d2        ; CR channel
  373.  
  374.     move.l    (a5,d1.w*4),d0            ; cb_r cb_g
  375.     move.l    (Cr_gb_off,a5,d2.w*4),d1    ; cr_g cr_b
  376.  
  377.     move.w    d1,d2
  378.     swap    d1
  379.     add.w    d0,d1
  380.     swap     d0
  381.  
  382.     sub.w    d1,d0
  383.     sub.w    d1,d2
  384.  
  385.     ext.l    d1
  386.     add.l    #_clamp,d1
  387.  
  388. ; d0   cb_r      *2
  389. ; d1  (cr_g+cb_g) *2
  390. ; d2   cr_b      *2
  391.  
  392.     moveq    #0,d3            ; *lum++
  393.     moveq    #0,d4
  394.     move.b    (a2)+,d3
  395.     move.l    d1,a6            ; clamp table includes on L
  396.     add.l    d3,d3
  397.     add.l    d3,a6
  398.     move.w    (a6,d0.w),d3        ; r
  399.     lsl.l    #4,d3
  400.     move.w    (a6),d3            ; g
  401.     lsl.l    #4,d3
  402.  
  403.     move.b    (a2)+,d4        ; *lum++
  404.     move.l    d1,a6            ; clamp table includes on L
  405.     add.w    d4,d4
  406.     add.l    d4,a6
  407.     move.w    (a6,d2.w),d3        ; b
  408.     lsr.l    #4,d3
  409.     move.b    (a6),d3            ; g
  410.     lsr.l    #4,d3
  411.     move.w    d3,(a0)+        ; *row1++
  412.  
  413.  
  414.     moveq    #0,d3            ; *lum++
  415.     moveq    #0,d4
  416.     move.b    (a3)+,d3
  417.     move.l    d1,a6            ; clamp table includes on L
  418.     add.l    d3,d3
  419.     add.l    d3,a6
  420.     move.w    (a6,d0.w),d3        ; r
  421.     lsl.l    #4,d3
  422.     move.w    (a6),d3            ; g
  423.     lsl.l    #4,d3
  424.  
  425.     move.b    (a3)+,d4        ; *lum++
  426.     move.l    d1,a6            ; clamp table includes on L
  427.     add.w    d4,d4
  428.     add.l    d4,a6
  429.     move.w    (a6,d2.w),d3        ; b
  430.     lsr.l    #4,d3
  431.     move.b    (a6),d3            ; g
  432.     lsr.l    #4,d3
  433.     move.w    d3,(a1)+        ; *row1++
  434.  
  435.  
  436.     dbra    d6,all_cols_12bit
  437.  
  438.     move.l    savecols(sp),d0        ; next line in the luminance channel
  439.     add.l    d0,a2
  440.     add.l    d0,a3
  441.  
  442.     add.l    d0,a0            ; next line in the output row
  443.     add.l    d0,a1
  444.  
  445.     dbra    d7,all_rows_12bit
  446.  
  447.     add.w    #localvars,sp
  448.     movem.l    (sp)+,cdi_regs
  449.     rts
  450.  
  451. ;************************************************************************************
  452.  
  453.     SECTION    __MERGED,BSS
  454.  
  455. ;
  456. ; uv conversion table; contains 4 sets describing the relation between the two
  457. ; chrominance channels and the four-times bigger luminance channel.
  458. ;
  459. Cb_r_tab: ds.l    2*256
  460.  
  461.  
  462. ; clamp table
  463. ;
  464. ;    clamp[x]    = 0xff00
  465. ; *(&clamp[x]-1) = 0x00ff
  466. ;
  467. ; an offset into this clamp table could very easily implement overall brightness control!
  468. ;
  469. ; we can reuse this special construct in video.c/sutils.s
  470. ;
  471.  
  472.     XDEF _clamp
  473.  
  474.  
  475.     ds.w    clampsize
  476. _clamp:    ds.w    256+clampsize
  477.  
  478.  
  479.     END
  480.